<?php
namespace Admin\Controller;
use Think\Controller;

/**
* 
*/
class VerifyController extends Controller{
	public function voice(){

		$page_no = I('get.p') ? I('get.p') : 1;
        $page_size = $_GET['page_size'] ? $_GET['page_size'] : 10;
		$data = M('user')->page($page_no, $page_size)->where('voice_id is not null and voice_status = 0')->select();
		foreach ($data as $key => $value) {
			if($value['sex'] == 1){
				$data[$key]['sex'] = '男';
			}elseif ($value['sex'] == 2) {
				$data[$key]['sex'] = '女';
			}else{
				$data[$key]['sex'] = '未知';
			}

			//是否认证
			if($value['is_verify'] == 1){
				$data[$key]['is_verify'] = '已认证';
			}else{
				$data[$key]['is_verify'] = '未认证';
			}
		}
		$count =  M('user')->where('voice_id is not null and voice_status = 0')->count();
		 $Page = new \AntAge\Page($count, $page_size);
        // 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page->show();
        // 分页显示输出
        $this->assign('page', $show);
        // 赋值分页输出
        $this->assign("count", $count);
		$this->assign('data',$data);
		$this->display();
	}
	//通过审核
	public function verify_voice(){
		$id = I('id');
		$res = M('user')->where('id='.$id)->save(array('voice_status'=>1));
		if($res === false){
			$this->ajaxReturn(array('status'=>0,'info'=>'审核失败'));
		}
		$content = '您的语音问候审核已通过，大家可以听到你的声音啦！';
		A('Admin/Message')->addSysMsg($id,$content);
		$this->ajaxReturn(array('status'=>1,'info'=>'审核成功'));
	}

	//通过审核
	public function not_verify_voice(){
		$id = I('id');
		$res = M('user')->where('id='.$id)->save(array('voice_status'=>2));
		if($res === false){
			$this->ajaxReturn(array('status'=>0,'info'=>'失败,请稍后刷新重试'));
		}
		$content = '由于语音内容存在一些问题，您的语音问候审核未通过，请重新上传！';
		A('Admin/Message')->addSysMsg($id,$content);
		$this->ajaxReturn(array('status'=>1,'info'=>'驳回成功'));
	}

	// 认证页面 user_auht 为主
	public function images1(){
		session('page_size',$_GET['page_size']);
        $search = $_GET['user_name'];
		$page_no = I('get.p') ? I('get.p') : 1;
        $page_size = $_GET['page_size'] ? $_GET['page_size'] : 10;

        // $where['b.username'] = array('like','%'.$search.'%');
        $where['_string'] = ' (b.username like "%'.$search.'%")  OR ( b.phone like "%'.$search.'%") ';
		$data = M('user_auth a')
			->where('a.add_time>0')
			->page($page_no, $page_size)
			->where($where)
			->field('a.* , b.username,b.is_verify,b.phone')
			->join('left join zm_user b on a.user_id=b.id')
			->order('id desc')
			->select();
			// dump($where);exit();
		foreach ($data as $k => $v) {
			if ($v['is_verify']==1) 
			{
				$data[$k]['is_verifys'] = '已认证';
			}else{
				$data[$k]['is_verifys'] = '未认证';
			}
		}
		$count =  M('user_auth a')->where($where)->join('left join zm_user b on a.user_id=b.id')->where('a.add_time>0')->count();
		$Page = new \AntAge\Page($count, $page_size);
        // 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page->show();
        // 分页显示输出
        $this->assign('page', $show);
        // 赋值分页输出
        $this->assign("count", $count);
		$this->assign('data',$data);
		$this->display();
	}

	public function images(){
		session('page_size',$_GET['page_size']);
        $search = $_GET['user_name'];
		$page_no = I('get.p') ? I('get.p') : 1;
        $page_size = $_GET['page_size'] ? $_GET['page_size'] : 10;

        // $where['a.username'] = array('like','%'.$search.'%');
        $where['_string'] = ' (a.username like "%'.$search.'%")  OR ( a.phone like "%'.$search.'%") ';
        $where['a.photo_id'] = array('neq','null');
        $where['a.id'] = array('not in','1,2,3,271,272,273,283');
        // dump($where);exit();
		$data = M('user a')
			->page($page_no, $page_size)
			->where($where)
			->field('a.id as user_id,b.image,b.images,a.photo_id,a.id,a.username,a.is_verify,a.photo_ok,a.phone,a.add_time')
			->join('left join zm_user_auth b on a.id=b.user_id')
			->order('is_verify desc ,id desc')
			->select();
			// dump($data);
		foreach ($data as $k => $v) {
			if ($v['is_verify']==1) 
			{
				$data[$k]['is_verifys'] = '已认证';
			}else{
				$data[$k]['is_verifys'] = '未认证';
			}
		}
		$count =  M('user a')->where($where)->join('left join zm_user_auth b on a.id=b.user_id')->count();
		$Page = new \AntAge\Page($count, $page_size);
        // 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page->show();
        // 分页显示输出
        $this->assign('page', $show);
        // 赋值分页输出
        $this->assign("count", $count);
		$this->assign('data',$data);
		$this->display();
	}

	//取消认证审核
	public function no_image_pass(){
		$id = I('id');
		$res = M('user')->where('id='.$id)->save(array('is_verify'=>0,'star'=>'','sex'=>'','phone'=>'','age'=>'','photo_id'=>''));
		M('user_auth')->where(array('user_id'=>$id))->delete();
		M('photos')->where(array('user_id'=>$id))->delete();
		$data = A('Home/Face')->admin_remove($id);
		if (!$data['results']) 
		{
			$this->error($data['message']);
		}
		if($res)
		{
			$this->success('取消成功');
		}else{
			$this->error('审核失败');
		}
		
	}

	public function test(){

		@unlink(ROOT_PATH . $cat_info["icon_pic"]);dump(111);exit();
		$content = M('message')->where(array('shou_id'=>125,'status'=>0))->count();dump($content);exit();

		// S('err'.$user_id,1,300);
		$user_id = 4;
		dump(S('err'.$user_id));
		// if (S('err'.$user_id)<C('AUTH_NUMBER')) 
		if (S('err'.$user_id)<3) 
		{
			S('err'.$user_id,S('err'.$user_id)+1,300);
		}else{
			dump('111');exit();
		}

		dump($user_id);exit();
	}

	public function photo_pass(){
		$user_id = I('post.user_id');
		if(empty($user_id)){
			$this->ajaxReturn(array('status' => -1, 'message' => 'user_id不能为空'));
		}
		$map['id'] = $user_id;
		$user = M('user')->field('id as user_id,username,photo_id,photo_ok')
		                 ->where($map)->find();
		if(!$user){
			$this->ajaxReturn(array('status' => -1, 'message' => '用户不存在'));
		}
		if(!$user['photo_id']){
			$this->ajaxReturn(array('status' => -1, 'message' => '用户还未上传头像'));
		}
		if(1 == $user['photo_ok']){
			$this->ajaxReturn(array('status' => 1, 'message' => '已审核通过'));
		}

		$res = M('user')->where($map)->save(array('photo_ok' => 1));
		if($res === false){
			$this->ajaxReturn(array('status' => -1, 'message' => '审核失败'));
		}
		$this->ajaxReturn(array('status' => 1, 'message' => '已审核通过'));
	}
	public function photo_not_pass(){
		$user_id = I('post.user_id');
		if(empty($user_id)){
			$this->ajaxReturn(array('status' => -1, 'message' => 'user_id不能为空'));
		}
		$map['id'] = $user_id;
		$user = M('user')->field('id as user_id,username,photo_id,photo_ok')
		                 ->where($map)->find();
		if(!$user){
			$this->ajaxReturn(array('status' => -1, 'message' => '用户不存在'));
		}
		if(!$user['photo_id']){
			$this->ajaxReturn(array('status' => -1, 'message' => '用户还未上传头像'));
		}
		if(2 == $user['photo_ok']){
			$this->ajaxReturn(array('status' => 1, 'message' => '已被驳回'));
		}

		$res = M('user')->where($map)->save(array('photo_ok' => 2, 'photo_id' => ''));

		if(false === $res){
			$this->ajaxReturn(array('status' => -1, 'message' => '操作失败'));
		}else{
			$content = '您上传的头像没有通过审核，请重新上传头像';
			A('Message')->addSysMsg($user_id,$content);
			$this->ajaxReturn(array('status' => 1, 'message' => '驳回成功'));
		}
	}
}